package com.SouthernWall_404.AgeRight.Client.Gui.Overlay;

import com.SouthernWall_404.AgeRight.Common.API.Capability.Missions;
import com.SouthernWall_404.AgeRight.Common.API.Capability.Provider.MissionsProvider;
import com.SouthernWall_404.AgeRight.Common.API.MissionAPI.AbstractMissionInfo;
import com.SouthernWall_404.AgeRight.Common.API.MissionAPI.MainMissionInfo;
import com.SouthernWall_404.AgeRight.Common.AgeRight;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.client.gui.overlay.ForgeGui;
import net.minecraftforge.client.gui.overlay.IGuiOverlay;

import java.util.ArrayList;
import java.util.List;

public class MissionOverlay implements IGuiOverlay {

    private static final int SIDEBAR_WIDTH = 130;
    private static final int HEADER_HEIGHT = 25;
    private static final int ITEM_HEIGHT = 22;
    private static final int PADDING = 8;

    private static long lastUpdateTime = 0;
    private static List<StatItem> cachedStats = new ArrayList<>();


    private static final int TitleHeight=16;
    private static final int TitleLength=96;
    private static final int logHeight=15;
    private static final int logLength=100;
    private static final int lamp=16;


    @Override
    public void render(ForgeGui forgeGui, GuiGraphics guiGraphics, float v, int length, int height) {
        // 计算物品栏位置


        this.renderMovieBlocker(guiGraphics,length,height);
        // 侧边栏位置 (物品栏右侧)
        int sidebarX = 0+TitleHeight/2;
        int sidebarY = height-logHeight;

//        // 每5秒更新一次统计数据
//        if (System.currentTimeMillis() - lastUpdateTime > 5000) {
//            lastUpdateTime = System.currentTimeMillis();
//        }

        Player player=Minecraft.getInstance().player;
        if(player!=null)
        {
            Missions missions=player.getCapability(MissionsProvider.MISSIONS_CAPABILITY).orElse(null);
            if(missions!=null) {
                List<MainMissionInfo> missionInfos=missions.getActiveMissions();

                if(!missionInfos.isEmpty())
                {
//                    missionInfos.get(0).getDescribe();

                    for(int i=missionInfos.get(0).missions.size()-1;i>=0;i--)
                    {
                        AbstractMissionInfo missionInfo=missionInfos.get(0).missions.get(i);
                        renderDialog(guiGraphics, sidebarX, sidebarY, missionInfo.getDescribe());
                        sidebarY -= TitleHeight;
                    }

                    renderTitle(guiGraphics, sidebarX, sidebarY,missionInfos.get(0).getDescribe());
                }
            }
        }


//        // 渲染标题
//        renderTitle(guiGraphics, sidebarX, sidebarY);
//
//        // 渲染统计项
//        renderStats(guiGraphics, sidebarX, sidebarY);
    }

    private void renderMovieBlocker(GuiGraphics guiGraphics, int length, int height)
    {
        int blockHeight=(int)(0.1*height);
        guiGraphics.fill(0, 0, length,blockHeight, 0xFF000000);
        guiGraphics.fill(0, height-blockHeight, length,height, 0xFF000000);
    }
    private static void renderTitle(
            GuiGraphics guiGraphics,
            int x, int y,Component component
    ) {
        // 标题文本
        Component title = component.copy().withStyle(ChatFormatting.WHITE);



        int length=Minecraft.getInstance().font.width(title);
        int height=Minecraft.getInstance().font.lineHeight;
        guiGraphics.blit(
                new ResourceLocation(AgeRight.MODID,"textures/gui/title_background.png"),
                x-4,y-3,0,0,TitleLength,TitleHeight,TitleLength,TitleHeight);
        guiGraphics.drawString(
                Minecraft.getInstance().font,
                title,
                x,
                y,
                0xFFFFFF
        );
    }
    private static void renderDialog(
            GuiGraphics guiGraphics,
            int x, int y,Component component
    ) {
        // 标题文本
        x+=5;
        Component title = component.copy().withStyle(ChatFormatting.WHITE);
        guiGraphics.blit(new ResourceLocation(AgeRight.MODID,"textures/gui/rose_quartz_lamp.png"),x,y,0,0,lamp/2,lamp/2,16,16);
        guiGraphics.drawString(
                Minecraft.getInstance().font,
                title,
                x+lamp+2,
                y,
                0xFFFFFF
        );
    }

    private static void renderStatItem(
            GuiGraphics guiGraphics,
            int x, int y,
            StatItem stat
    ) {
        Font font = Minecraft.getInstance().font;

        // 渲染图标
        guiGraphics.renderItem(
                ItemStack.EMPTY,
                x,
                y
        );

        // 渲染标签
        guiGraphics.drawString(
                font,
                stat.label() + ":",
                x + 20,
                y + (ITEM_HEIGHT - font.lineHeight) / 2,
                0xDDDDDD
        );

        // 渲染数值
        int valueWidth = font.width(stat.value());
        guiGraphics.drawString(
                font,
                stat.value(),
                x + SIDEBAR_WIDTH - PADDING * 2 - valueWidth,
                y + (ITEM_HEIGHT - font.lineHeight) / 2,
                stat.color()
        );
    }

    private record StatItem(
            String label,
            Component value,
            int color,
            Item icon
    ) {}
}